// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <memory>
#include <stdexcept>

#include "gtest/gtest-message.h"
#include "gtest/gtest-test-part.h"
#include <gtest/gtest.h>
#include <pollux/testing/gtest_utils.h>
#include <pollux/type/hive/hive_type_serializer.h>

using kumo::pollux::TypeKind;

namespace kumo::pollux::type::hive {

TEST(HiveTypeSerializer, primitive) {
  std::shared_ptr<const pollux::Type> type = pollux::BIGINT();
  auto result = HiveTypeSerializer::serialize(type);
  EXPECT_EQ(result, "bigint");
}

struct Foo {};
TEST(HiveTypeSerializer, opaque) {
  // Use a custom name to highlight this is just an alias.
  registerOpaqueType<Foo>("bar");

  std::shared_ptr<const Type> type = OPAQUE<Foo>();
  auto result = HiveTypeSerializer::serialize(type);
  EXPECT_EQ(result, "opaque<bar>");
  unregisterOpaqueType<Foo>("bar");
}

TEST(HiveTypeSerializer, unregisteredOpaque) {
  // did not call registerOpaqueType<Foo>("Foo")
  std::shared_ptr<const Type> type = OPAQUE<Foo>();
  POLLUX_ASSERT_THROW(
      HiveTypeSerializer::serialize(type),
      fmt::format(
          "Could not find type index '{}'. Did you call registerOpaqueType?",
          type->as_opaque().typeIndex().name()));
}

TEST(HiveTypeSerializer, unsupported) {
  std::shared_ptr<const Type> type = UNKNOWN();
  POLLUX_ASSERT_THROW(
      HiveTypeSerializer::serialize(type), "unsupported type: UNKNOWN");
}
} // namespace kumo::pollux::type::hive
